package www.com.library.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.util.AttributeSet;
import android.view.View;

import www.com.library.util.DeviceUtil;

/**
 * @author jett
 * @since 2018-01-16.
 */
public class XRecyclerHeaderProgress extends View {

    private Paint paint = new Paint();
    private Point center = new Point();
    private int radius;

    private int borderWidth;
    private float max;
    private float progress;
    private int centerRectCornor;

    private RectF mArcRect = new RectF();
    private RectF mCenterRect = new RectF();
    private Xfermode mode = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);

    public XRecyclerHeaderProgress(Context context) {
        super(context);
        init();
    }

    public XRecyclerHeaderProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public XRecyclerHeaderProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        borderWidth = DeviceUtil.instance().dip2px(1F, getContext());
        centerRectCornor = DeviceUtil.instance().dip2px(1F, getContext());
        max = 100;
        progress = 66;

        paint.setAntiAlias(true);
        paint.setColor(Color.GRAY);
    }

    public void setBorderColor(int color) {
        paint.setColor(color);
    }

    public void setMax(float total) {
        this.max = total;
    }

    public void setProgress(float progress) {
        this.progress = progress;
        postInvalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        if (measuredWidth > 0 && measuredHeight > 0) {
            center.x = measuredWidth / 2;
            center.y = measuredHeight / 2;

            int realSize = Math.min(measuredWidth, measuredHeight) / 2;
            radius = realSize - borderWidth;

            mArcRect.left = center.x - realSize;
            mArcRect.top = center.y - realSize;
            mArcRect.right = center.x + realSize;
            mArcRect.bottom = center.y + realSize;

            int centerRectRadio = realSize / 4;
            centerRectCornor = centerRectRadio / 3;

            mCenterRect.left = center.x - centerRectRadio;
            mCenterRect.top = center.y - centerRectRadio;
            mCenterRect.right = center.x + centerRectRadio;
            mCenterRect.bottom = center.y + centerRectRadio;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (radius <= 0) {
            return;
        }
        int angle = calcAngle();

        // 绘制圆环
        int layer = canvas.saveLayer(mArcRect, paint, Canvas.ALL_SAVE_FLAG);
        canvas.drawArc(mArcRect, 270, angle, true, paint); // 先画个扇形出来
        paint.setXfermode(mode);
        canvas.drawCircle(center.x, center.y, radius, paint); // 再画个圆
        paint.setXfermode(null);
        canvas.restoreToCount(layer);

        //再画扇形二头的圆角
        canvas.drawCircle(center.x, borderWidth / 2, borderWidth / 2, paint);
        Point point = calcDistanceCenter(angle);
        canvas.drawCircle(point.x, point.y, borderWidth / 2, paint);

        //最后画中间的小方块
        canvas.rotate(angle, center.x, center.y);
        canvas.drawRoundRect(mCenterRect, centerRectCornor, centerRectCornor, paint);
    }

    private Point calcDistanceCenter(int angle) {
        int dx = (int) (Math.sin(angle * Math.PI / 180) * (radius + borderWidth / 2));
        int dy = (int) (Math.cos(angle * Math.PI / 180) * (radius + borderWidth / 2));
        Point point = new Point();
        point.x = center.x + dx;
        point.y = center.y - dy;
        return point;
    }

    private int calcAngle() {
        if (max <= 0) {
            return 0;
        }
        if (progress >= max) {
            return 360;
        }
        return (int) (progress / max * 360);
    }

}
